package com.multi.tool.guava.compare;

import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Lists;

import java.util.Comparator;
import java.util.List;

/**
 * 多条件过滤比较器
 */
public class CompoundComparator<T> implements Comparator<T> {

    private List<Comparator<T>> comparators;

    @Override
    public int compare(T o1, T o2) {
        ComparisonChain chain = ComparisonChain.start();
        for (Comparator<T> comparator : comparators) {
            chain = chain.compare(o1, o2, comparator);
        }
        return chain.result();
    }

    public void setComparator(Comparator<T> comparator) {
        if (null == this.comparators) {
            this.comparators = Lists.newArrayList();
        }
        this.comparators.add(comparator);
    }

}
